﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tools.Framework
{
    public static class ConvertHelper
    {
        public static T ConverTo<T>(this IConvertible obj, T defualtvalue = default(T))
        {
            if (obj == null || obj is DBNull || obj=="")
                return defualtvalue;
            var type = typeof (T);
            if (type.IsNullableType())
            {
                return (T)Convert.ChangeType(obj,Nullable.GetUnderlyingType(typeof(T)));
            }
            else
            {
                T t = (T)Convert.ChangeType(obj, typeof(T));
                return t;
            }
        }

        public static bool IsNullableType(this Type theType)
        {
            return theType.IsGenericType && theType.GetGenericTypeDefinition() == typeof(Nullable<>);
        }  
    }
}
